package main

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"log"
	"websocket_exam/api"
	"websocket_exam/api/handler"
	"websocket_exam/config"
	"websocket_exam/consts"
	"websocket_exam/inits"
	"websocket_exam/internal/queue"
)

func main() {
	router := gin.Default()
	config.Init()
	inits.InMysql()
	inits.InitRabbitMq()
	router.Static("/upload", "D:\\GO-Work\\src\\websocket_exam\\static\\upload\\")
	go SendMessage()
	api.LoadRouters(router)
	router.Run() // 监听并在 0.0.0.0:8080 上启动服务
}

func SendMessage() {

	msgs := queue.PublishSub(consts.SEND_GROUP)

	forever := make(chan bool)
	//启用协程处理消息
	go func() {
		for d := range msgs {
			//消息逻辑处理，可以自行设计逻辑
			log.Printf("Received a message: %s", d.Body)
			var data string
			_ = json.Unmarshal(d.Body, &data)

			for _, conn := range handler.OnlineUser {
				handler.WSRespSuccess(conn, data)
			}
		}
	}()

	log.Printf(" [*] Waiting for messages. To exit press CTRL+C")
	<-forever
}
